﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ESOL.Common.Entity;
using ESOL.Business.Mst;
using ESOL.Business.Inv;
using ESOL.Common;

namespace ESOL.WinUI.Inv
{
    public partial class frmManageDispatchOrder : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        private InvDispatchOrderEntity mSelectedDispatchOrder;
        #endregion

        #region  Constructor
        public frmManageDispatchOrder()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void BindComboBox()
        {
            List<MstWarehouseEntity> list = MstWarehouseBs.GetMstWarehouseAllList();
            MstWarehouseEntity entity = new MstWarehouseEntity();
            list.Insert(0, entity);
            cmbWareHouseCode.DataSource = list;
            cmbWareHouseCode.DisplayMember = "WarehouseName";
            cmbWareHouseCode.ValueMember = "WarehouseCode";

            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbOrderType, "IDT", true);
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "IDO", true);

            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(repositoryItemGridLookUpEdit1, "IDT", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(repositoryItemGridLookUpEdit2, "IDO", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(repositoryItemGridLookUpEdit3, "UNIT", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(repositoryItemGridLookUpEdit4, "UNIT", true);

            cmbStatus.SelectedValue = "IDO01";
            //cmbOrderType.SelectedValue = "IDT02";

        }

        private string GetCondition()
        {
            string condition = "";

            if (string.IsNullOrEmpty(txtOrderCode.Text) == false)
            {
                condition += " AND A.ORDER_CODE LIKE '%" + txtOrderCode.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtRelatedOrder.Text) == false)
            {
                condition += " AND A.RELATED_ORDER LIKE '%" + txtRelatedOrder.Text + "%'";
            }
            if (dtpOrderDate.EditValue!=null)
            {
                condition += " AND A.ORDER_DATE >= CONVERT(DATETIME,'" + dtpOrderDate.EditValue + "')";
            }
            if (dtpOrderDateTo.EditValue!=null)
            {
                condition += " AND A.ORDER_DATE <= CONVERT(DATETIME,'" + dtpOrderDateTo.EditValue + "')";
            }
            if (txtOrderPerson.Tag != null && txtOrderPerson.Tag.GetType() == typeof(MstPersonEntity))
            {
                MstPersonEntity entity = (MstPersonEntity)txtOrderPerson.Tag;
                condition += " AND A.CREATED_BY = '" + entity.PersonId + "' ";
            }
            if (string.IsNullOrEmpty(cmbWareHouseCode.Text) == false && cmbWareHouseCode.SelectedValue != null)
            {
                condition += " AND B.WAREHOUSE_CODE = '" + cmbWareHouseCode.SelectedValue.ToString() + "'";
            }
            if (string.IsNullOrEmpty(cmbStatus.Text) == false && cmbStatus.SelectedValue != null)
            {
                condition += " AND A.STATUS = '" + cmbStatus.SelectedValue.ToString() + "'";
            }
            if (string.IsNullOrEmpty(cmbOrderType.Text) == false && cmbOrderType.SelectedValue != null)
            {
                condition += " AND A.ORDER_TYPE = '" + cmbOrderType.SelectedValue.ToString() + "'";
            }
            return condition;
        }

        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "A.CREATED_DATE DESC";
            DataTable dt = InvDispatchOrderBs.GetByCondition(condition, orderBy, rowCount);
            gvMain.DataSource = dt;
        }

        private void BindDetailGrid()
        {
            string sOrderID = mSelectedDispatchOrder.DispatchOrderId;
            if (string.IsNullOrEmpty(sOrderID))
            {
                gvDetail.DataSource = null;
                return;
            }
            DataSet ds = InvDispatchOrderBs.GetDataSetByDispatchOrderId(sOrderID, mSelectedDispatchOrder.OrderType);
            if (ds == null || ds.Tables.Count == 0)
            {
                gvDetail.DataSource = null;
            }
            gvDetail.DataSource = ds.Tables[0];
        }

        private void ResetButtonsEnable()
        {
            btnNew.Enabled = false;
            btnModify.Enabled = false;
            btnDelete.Enabled = false;
            btnSubmit.Enabled = false;
            btnApprove.Enabled = false;
            if (mSelectedDispatchOrder == null)
            {
                return;
            }
            if (mSelectedDispatchOrder.Status == "IDO01")//待审核
            {
                btnModify.Enabled = true;
                btnDelete.Enabled = true;
                btnApprove.Enabled = true;
            }
            if (mSelectedDispatchOrder.Status == "IDO02")//已审核
            {
                btnModify.Enabled = false;
                btnDelete.Enabled = false;
                btnApprove.Enabled = false;
            }
            if (mSelectedDispatchOrder.Status == "IDO03")//已出库
            {
                btnModify.Enabled = false;
                btnDelete.Enabled = false;
                btnApprove.Enabled = false;
            }
            if (mSelectedDispatchOrder.Status == "IDO04")//已拒绝
            {
                btnModify.Enabled = true;
                btnDelete.Enabled = true;
                btnApprove.Enabled = true;
            }
            if (mSelectedDispatchOrder.Status == "IDO05")//已删除
            {
                btnModify.Enabled = false;
                btnDelete.Enabled = false;
                btnApprove.Enabled = false;
            }

        }
        #endregion 

        #region Events
        private void frmManagedispatchOrder_Load(object sender, EventArgs e)
        {
            BindComboBox();
            BindDataGrid();
        }

        DataRow drSelected;
        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            drSelected = gridView1.GetDataRow(e.RowHandle);
            mSelectedDispatchOrder = new InvDispatchOrderEntity(drSelected);
            BindDetailGrid();
            ResetButtonsEnable();
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }
        protected override void Click_View()
        {
            if (mSelectedDispatchOrder == null)
            {
                MessageBox.Show("请选择一个出库申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainDispatchOrder frm = new frmMaintainDispatchOrder();
            frm.ThisActionType = ActionType.View;
            frm.SelectedDispatchOrder = mSelectedDispatchOrder;
            frm.ShowDialog();
        }
        protected override void Click_New()
        {
            base.Click_New();
        }
        protected override void Click_Delete()
        {
            if (mSelectedDispatchOrder == null)
            {
                MessageBox.Show("请选择一个发货申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否删除选中发货申请单？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {

                mSelectedDispatchOrder.Status = "IDO05";
                mSelectedDispatchOrder.LastModifiedBy = CommonParameter.CurrentUser.PersonId;
                mSelectedDispatchOrder.LastModifiedDate = DateTime.Now;
                bool successful = InvDispatchOrderBs.UpdateInvDispatchOrder(mSelectedDispatchOrder);
                if (successful)
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        protected override void Click_Modify()
        {
            if (mSelectedDispatchOrder == null)
            {
                MessageBox.Show("请选择一个出库申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainDispatchOrder frm = new frmMaintainDispatchOrder();
            frm.ThisActionType = ActionType.Modify;
            frm.SelectedDispatchOrder = mSelectedDispatchOrder;
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                BindDataGrid();
            }
        }
        protected override void Click_Approve()
        {
            if (mSelectedDispatchOrder == null)
            {
                MessageBox.Show("请选择一个出库申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainDispatchOrder frm = new frmMaintainDispatchOrder();
            frm.ThisActionType = ActionType.Approve;
            frm.SelectedDispatchOrder = mSelectedDispatchOrder;
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                BindDataGrid();
            }
        }
        protected override void Click_Print()
        {
            if (drSelected == null)
            {
                MessageBox.Show("请选择一张出库单！","Information",MessageBoxButtons.OK,MessageBoxIcon.Information);
                return;
            }
            DataTable dt=gvDetail.DataSource as DataTable;
            frmPrintFormInvDispatchOrder frm = new frmPrintFormInvDispatchOrder(drSelected, dt);
            frm.Show();
        }
        private void btnLocationChange_Click(object sender, EventArgs e)
        {

        }

        #endregion

    }
}